<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--[if lt IE9]>
    <script src="lib/html5shiv-3.7.3/src/html5shiv.js"></script>
    <![endif]-->
    <script src="lib/Respond-1.4.2/src/respond.js"></script>
</head>
<body>
<!-- 使用canvas 实现简单的摄像头调用以及拍照 -->


<video id="video" width="640" height="480" autoplay></video>
<button id="snap">Start</button>  <!-- 开始拍照 -->
<button id="picture">拍照片</button>
<button id="refresh">刷新页面</button>
<canvas id="canvas" width="640" height="480"></canvas>

<script type="text/javascript">
    window.addEventListener("DOMContentLoaded", function () {
        var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        var video = document.getElementById("video");
        var snap = document.getElementById("snap");
        var picture = document.getElementById("picture");
        var refresh = document.getElementById("refresh");


        var videoObj = {"video": true},
                errBack = function (error) {
                    console.log("Video capture error: ", error.code);
                };
        snap.addEventListener("click", function () {
            if (navigator.getUserMedia) {
                navigator.getUserMedia(videoObj, function (stream) {
                    video.src = stream;
                    video.play();
                }, errBack);
            } else if (navigator.webkitGetUserMedia) {
                navigator.webkitGetUserMedia(videoObj, function (stream) {
                    video.src = window.webkitURL.createObjectURL(stream);
                    video.play();
                }, errBack);
            }
            else if (navigator.mozGetUserMedia) {
                navigator.mozGetUserMedia(videoObj, function (stream) {
                    video.src = window.URL.createObjectURL(stream);
                    video.play();
                }, errBack);
            }
        });
        picture.addEventListener("click", function () {
            context.drawImage(video, 0, 0, 640, 480);
        });
//        refresh.addEventListener("click", function () {
//            document.location.reload();
//        });
    }, false);
</script>
</body>
</html>